package com.pansoft.mobilefitness.view;

/**
 * Author by cimu
 * Date on 2019/1/29.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.pansoft.mobilefitness.R;

/**
 * Created by Administrator on 2018/5/13.
 * 运动圆环自定义View
 */

public class MotionCrcle extends View {
    /**
     * 外部圆环颜色
     */
    private int mOuterColor = Color.GRAY;
    /**
     * 里面圆环颜色
     */
    private int mInnerColor = Color.RED;
    /**
     * 跑步数的文字大小
     */
    private int mTextSize = 30;
    /**
     * 跑步文字的颜色
     */
    private int mTextColor = Color.BLACK;
    /**
     * 圆环的宽度
     */
    private int mCrcleWidth = 25;
    private Paint mOuterPaint, mInnerPaint, mTextPaint;
    private float mMaxSteps = 0;
    private int mCurrentSteps = 0;
    public MotionCrcle(Context context) {
        this(context, null);
    }

    public MotionCrcle(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MotionCrcle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MotionCrcle);
        mOuterColor = array.getColor(R.styleable.MotionCrcle_outerCrcleColor, mOuterColor);
        mInnerColor = array.getColor(R.styleable.MotionCrcle_innerCrcleColor, mInnerColor);
        mCrcleWidth = array.getInt(R.styleable.MotionCrcle_crcleWidth, mCrcleWidth);
        mTextColor = array.getColor(R.styleable.MotionCrcle_crcleTextColor, mTextColor);
        mTextSize = array.getInt(R.styleable.MotionCrcle_crcleTextSize, mTextSize);
        initPaint();
        array.recycle();
    }

    public synchronized void setmMaxSteps(float mMaxSteps) {
        this.mMaxSteps = mMaxSteps;
    }

    public synchronized void setmCurrentSteps(int mCurrentSteps) {
        this.mCurrentSteps = mCurrentSteps;
        invalidate();
    }

    private void initPaint() {
        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setStrokeWidth(mCrcleWidth);
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);
        mOuterPaint.setStyle(Paint.Style.STROKE);
        mOuterPaint.setColor(mOuterColor);

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mCrcleWidth);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        mInnerPaint.setStyle(Paint.Style.STROKE);
        mInnerPaint.setColor(mInnerColor);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if(width != height){
            /**
             * 因为运动圆环是一个正方形，所以我们要设置一个最小值作为View的长度
             */
            int min = Math.min(width, height);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(min, MeasureSpec.EXACTLY);
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //因为圆环本身是具有宽度的，所以我们这里左边的顶点要往右mCrcleWidth个距离，右边底部的点要往左，所以要减去mCrcleWidth
        RectF recf = new RectF(mCrcleWidth, mCrcleWidth,
                getWidth() - mCrcleWidth, getHeight() - mCrcleWidth);
        //135是外部圆环的初始角度，270是所画的总角度数
        canvas.drawArc(recf, 135, 270, false, mOuterPaint);
        if(mCurrentSteps == 0){
            return;
        }
        //算出当前步数是最大步数的比值
        float angle = mCurrentSteps / mMaxSteps;
        canvas.drawArc(recf, 135, 270 * angle, false, mInnerPaint);
        String stepText = "";
        Rect bounds = new Rect();
        //算出步数文字的baseLine，也就是基准线
        mTextPaint.getTextBounds(stepText, 0, stepText.length(), bounds);
        int x = getWidth() / 2 - bounds.width() / 2;
        Paint.FontMetricsInt metrices = mTextPaint.getFontMetricsInt();
        int diffY = (metrices.bottom - metrices.top) / 2 - metrices.bottom;
        int baseLine = getHeight() / 2 + diffY;
        canvas.drawText(stepText, x, baseLine, mTextPaint);
    }

}